package org.gen.pojo.util;

import java.io.File;

import org.gen.pojo.config.GenConfig;

/**
 * Created by zhuge on 2017/10/11.
 */
public class MavenModuleUtil {
	// 获取maven根目录
	public static String getModuleRootPath() {
		String path = MavenModuleUtil.class.getResource(".").getPath();
		return path.substring(0, path.indexOf("target"));
	}

	// 包名转换为路径
	public static String packageToDir(String packageName) {
		String[] split = packageName.split("\\.");
		StringBuilder ret = new StringBuilder();
		for (String str : split) {
			ret.append(str).append("/");
		}
		return ret.toString();
	}

	public static void clearModuleResource(String folderName) {
		clear(folderName, "src/main/resources/");
	}

	public static void clearModuleJavaSource(String folderName) {
		clear(folderName, "src/main/java/");
	}

	public static void clear(String folderName, String target) {
		// TODO Auto-generated method stub
		String rootPath = getModuleRootPath();
		File dir = new File(rootPath + target + packageToDir(folderName));
		boolean flag = deleteFile(dir);
		System.out.println("清理标记" + flag);
		System.out.println("目录已清理" + rootPath + target + packageToDir(folderName));
	}

	/**
	 * 先根遍历序递归删除文件夹
	 *
	 * @param dirFile
	 *            要被删除的文件或者目录
	 * @return 删除成功返回true, 否则返回false
	 */
	public static boolean deleteFile(File dirFile) {
		// 如果dir对应的文件不存在，则退出
		if (!dirFile.exists()) {
			return false;
		}

		if (dirFile.isFile()) {
			return dirFile.delete();
		} else {

			for (File file : dirFile.listFiles()) {
				deleteFile(file);
			}
		}

		return dirFile.delete();
	}

	public static void main(String[] args) {
		/**
		 *
		 * /C:/apache/pinyougou/pinyougouparent/generatePinyougou/
		 */
		String path = MavenModuleUtil.getModuleRootPath();
		System.out.println(path);
		System.out.println(System.getProperty("user.dir"));
		// 获得项目根目录的绝对路径
		System.out.println(System.getProperty("java.class.path")); // 得到类路径和包路径
	}

}
